$(function () {
  var $tbClients = $('#tbClients'), $pager = $('#tbClientsPager');
  var $tbCars = $('#tbCars'), $pager = $('#tbCarsPager');

  // 调出模态框的时候去加载数据
  $('#modal-tbClients').on('show.bs.modal', function (e) {
    //加载数据
    $tbClients.jqGrid({
      url: addworksingleform_urls.getClients,
      datatype: 'json',
      mttype: 'post',
      colNames: ['姓名', '地址', '电话', '操作'],
      colModel: [
        { name: 'name', index: 'name', width: 100, align: 'center' },
        { name: 'address', index: 'address', width: 200, align: 'center' },
        { name: 'phone', index: 'phone', width: 160, align: 'center' },
        {
          name: 'id', index: 'id', width: 50, align: 'center',
          formatter: function (value, options, obj) {
            return '<a style="cursor:pointer;" onclick="addWorkSingleForm.chooseClient(' + value + ',\'' + obj.name + '\');">选择</a>';
          }
        },
      ],
      height: 'auto',
      pager: '#tbClientsPager',
      // viewrecords: true,
      rowNum: 10,
      rowList: [10, 20, 40],
      autowidth: true
    });
    // 隐藏水平方向滚动条
    $tbClients.closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });
  });



  // 调出模态框的时候去加载数据
  $('#modal-tbCars').on('show.bs.modal', function (e) {
    //加载数据
    $tbCars.jqGrid({
      url: addworksingleform_urls.getCars,
      datatype: 'json',
      mttype: 'post',
      colNames: ['车牌号', '车型', '车架号', '操作'],
      colModel: [
        { name: 'car_num', index: 'car_num', width: 100, align: 'center' },
        { name: 'car_type', index: 'car_type', width: 200, align: 'center' },
        { name: 'vin', index: 'vin', width: 160, align: 'center' },
        {
          name: 'car_num', index: 'car_num', width: 50, align: 'center',
          formatter: function (value, options, obj) {
            return '<a style="cursor:pointer;" onclick="addWorkSingleForm.chooseCars(\'' + value + '\');">选择</a>';
          }
        },
      ],
      height: 'auto',
      pager: '#tbCarsPager',
      // viewrecords: true,
      rowNum: 10,
      rowList: [10, 20, 40],
      autowidth: true
    });
    // 隐藏水平方向滚动
    $tbCars.closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });
  });
});
addWorkSingleForm = {
  // 查询客户
  search: function () {
    var $fmQuery = $('.fmClientQuery-hook');
    var param = ittaoli.serializeObject($fmQuery);
    $('#tbClients').jqGrid('setGridParam', {
      postData: { q: JSON.stringify(param) },
      page: 1
    }).trigger("reloadGrid");
  },
  // 清空查询条件
  searchNull: function () {
    $('.fmClientQuery-hook input').val('');
    var param = {};
    $('#tbClients').jqGrid('setGridParam', {
      postData: { q: JSON.stringify(param) },
      page: 1
    }).trigger("reloadGrid");
  },
  // 添加客户
  addClient: function (obj) {
    var $fmAdd = $('.fmClientAdd-hook');
    var param = ittaoli.serializeObject($fmAdd);
    param.id = 0;

    if (param.name == '') {
      bootbox.alert('客户姓名不能为空!');
      return;
    }
    if (!ittaoli.regs.phone.test(param.phone)) {
      bootbox.alert('手机号码格式不正确!');
      return;
    }

    $(obj).button('loading');
    $.post(addworksingleform_urls.addClient, { param: JSON.stringify(param) }, function (data) {
      if (data > 0) {
        $('#fmWork input[name=client_name]').val(param.name);
        $('#fmWork input[name=client_id]').val(data);
        $('#tbClients').jqGrid().trigger('reloadGrid');
        $('#modal-tbClients').modal('hide');
      } else {
        bootbox.alert('保存失败！');
      }
      $(obj).button('reset');
      $fmAdd[0].reset();
    })
  },
  // 选择客户
  chooseClient: function (id, name) {
    $('#fmWork input[name=client_id]').val(id);
    $('#fmWork input[name=client_name]').val(name);
    $('#modal-tbClients').modal('hide');
  },

  // 查询车辆
  searchCar: function () {
    var $fmQuery = $('.fmCarsQuery-hook');
    var param = ittaoli.serializeObject($fmQuery);
    $('#tbCars').jqGrid('setGridParam', {
      postData: { q: JSON.stringify(param) },
      page: 1
    }).trigger("reloadGrid");
  },
  // 清空查询条件
  searchNullCar: function () {
    $('.fmCarsQuery-hook input').val('');
    var param = {};
    $('#tbCars').jqGrid('setGridParam', {
      postData: { q: JSON.stringify(param) },
      page: 1
    }).trigger("reloadGrid");
  },
  // 添加车辆
  addCar: function (obj) {
    var $fmAdd = $('.fmCarsAdd-hook');
    var param = ittaoli.serializeObject($fmAdd);
    param.car_num = param.car_num.replace(/\s/g, "");
    if (!ittaoli.regs.carPlate.test(param.car_num)) {
      bootbox.alert('车牌号格式不正确');
      return;
    }

    $(obj).button('loading');
    $.post(addworksingleform_urls.addCar, { param: JSON.stringify(param) }, function (data) {
      if (data > 0) {
        $('#fmWork input[name=car_num]').val(param.car_num);
        $('#tbCars').jqGrid().trigger('reloadGrid');
        $('#modal-tbCars').modal('hide');
      } else {
        bootbox.alert('保存失败！');
      }
      $(obj).button('reset');
      console.log($fmAdd);
      $fmAdd[0].reset();
    })
  },
  // 选择车辆
  chooseCars: function (car_num) {
    $('#fmWork input[name=car_num]').val(car_num);
    // 根据车牌号去查找最近一次该车牌来保养的客户信息
    $.post(addworksingleform_urls.getLinkedClient, { 'car_num': car_num }, function (data) {
      if (data) {
        $('#fmWork input[name=client_name]').val(data.name);
        $('#fmWork input[name=client_id]').val(data.id);
      }
    });
    $('#modal-tbCars').modal('hide');
  },
  // 保存工单数据
  saveWork: function (obj) {
    var param = ittaoli.serializeObject($('#fmWork'));
    // console.log(param);
    if (param.num == '') {
      bootbox.alert('工单号不能为空哦~');
      return;
    }
    if (param.car_num == '') {
      bootbox.alert('车牌号不能为空哦~');
      return;
    }
    if (!param.reception_id) {
      bootbox.alert('请选择接待人员');
      return;
    }

    if (!ittaoli.regs.carKilo.test(param.mileage)) {
      bootbox.alert('公里数格式不对哦！');
      return;
    }


    if (param.client_name == '') {
      bootbox.alert('客户信息不能为空哦~');
      return;
    }

    $(obj).button('loading');
    $.post(addworksingleform_urls.addWork, { param: JSON.stringify(param) }, function (data) {
      if (data > 0) {
        bootbox.alert("保存成功！");
        setTimeout(function () {
          location.href = addworksingleform_urls.mainMenu;
        }, 1000);
      } else {
        bootbox.alert('保存失败！');
      }
      $(obj).button('reset');
    })
  }
};